package com.sdp.refund.controller;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.sdp.core.bizc.utils.UniqueCheckTools;
import com.sdp.core.bizc.vo.Result;
import com.sdp.refund.cache.RefundCleanCache;
import com.sdp.refund.db.refund_area.entity.RefundArea;
import com.sdp.refund.db.refund_area.service.IRefundAreaService;
import com.sdp.refund.utils.RefundConstants;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zsp
 * @since 2024-08-27
 */
@RestController
@RequestMapping("/RefundAreaController")
public class RefundAreaController {
	
	@Autowired
	private IRefundAreaService iRefundAreaService;
	
	@Autowired
	private RefundCleanCache refundCleanCache;
	
	@Autowired
	private UniqueCheckTools uniqueCheckTools;
	
	@PostMapping(value = "/queryAreaTableTree")
	public Result<?> queryAreaTableTree() {
		return Result.ok(iRefundAreaService.queryAreaTableTree());
	}
	
	@PostMapping(value = "/save")
	public Result<?> save(@RequestBody RefundArea area) {
		if(area==null) {
			return Result.error("小区信息为空");
		}
		//最多3层，进行判断
		if(!StringUtils.isEmpty(area.getParentArea())) {
			RefundArea parentArea = iRefundAreaService.queryAreaByCode(area.getParentArea());
			if(RefundConstants.单元.getCode().equals(parentArea.getAreaType())) {
				return Result.error("小区只支持3级");
			}else if(RefundConstants.小区.getCode().equals(parentArea.getAreaType())) {
				if(!RefundConstants.楼栋.getCode().equals(area.getAreaType())) {
					return Result.error("小区下级应该为楼栋");
				}
				
			}else if(RefundConstants.楼栋.getCode().equals(parentArea.getAreaType())) {
				if(!RefundConstants.单元.getCode().equals(area.getAreaType())) {
					return Result.error("楼栋下级应该为单元");
				}
			}
			
			
		}else {
			//如果父级为空，那只能是小区
			if(!RefundConstants.小区.getCode().equals(area.getAreaType())) {
				return Result.error("只有小区才能处于顶级");
			}
			
		}
	
		if(StringUtils.isEmpty(area.getKeyId())) {
			Result<?> r = uniqueCheckTools.isUniqueSingle(iRefundAreaService, RefundArea.class, new String[] {"areaCode"}, "keyId", area, null, true);
			if(r==null || !r.isSuccess()) {
				return Result.error("小区编号不能重复");
			}
			r = setLevel(area);
			if(r==null || !r.isSuccess()) {
				return r;
			}
			
			iRefundAreaService.save(area);
		}else {
			Result<?> r = uniqueCheckTools.isUniqueSingle(iRefundAreaService, RefundArea.class, new String[] {"areaCode"}, "keyId", null, area, false);
			if(r==null || !r.isSuccess()) {
				return Result.error("小区编号不能重复");
			}
			r = setLevel(area);
			if(r==null || !r.isSuccess()) {
				return r;
			}
			iRefundAreaService.updateById(area);
		}
		//清除小区缓存
		refundCleanCache.cleanAreaCache();
		return Result.ok();
		
	}
	
	/**
	 * 设置层级
	 * @param sysOrg
	 * @return
	 */
	private Result<?> setLevel(RefundArea area) {
		if(StringUtils.isEmpty(area.getParentArea())) {
			area.setAreaLevel("["+area.getAreaCode()+"]");
		}else if(area.getParentArea().equals(area.getAreaCode())){
			return Result.error("选择的父层级不能为自己");
		}else{
			RefundArea parentArea = iRefundAreaService.queryAreaByCode(area.getParentArea());
			if(parentArea==null) {
				return Result.error("选择的父层级已经不存在");
			}else if(parentArea.getAreaLevel().indexOf("["+area.getAreaCode()+"]")>-1) {
				return Result.error("选择的父层级不符合要求，存在相互依赖关系");
			}
			area.setAreaLevel(parentArea.getAreaLevel()+"["+area.getAreaCode()+"]");

		}
		return Result.ok();
	}


}

